﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tinkerc.Framework.Data;
using Tinkerc.Framework.Test.Model;

namespace Tinkerc.Framework.Test
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Insert()//插入一条数据
        {
            try
            {
                var model = new UserEntity()
                {
                    Id = 1002,
                    Name = "张三1",
                    Pwd = "123456",
                    Age = 15
                };

                using (var db = new DbContext())
                {
                    var result = db.Insert(model);
                    if (result)
                        Console.WriteLine("添加成功");
                    else
                        Console.WriteLine("添加失败");
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        [TestMethod]
        public void InsertBatch()//插入多条数据
        {
            var list = new List<UserEntity>();
            for (int i = 10; i < 21; i++)
            {
                var model = new UserEntity()
                {
                    Name = "张三1" + i,
                    Pwd = "123456" + i,
                    Age = 15 + i
                };
                list.Add(model);
            }
            using (var db = new DbContext())
            {
                var result = db.Insert(list);
                if (result)
                    Console.WriteLine("添加成功");
                else
                    Console.WriteLine("添加失败");
            }
        }

        [TestMethod]
        public void Delete()//插入多条数据
        {
            using (var db = new DbContext())
            {
                var result = db.Delete<UserEntity>(u => u.Id == 10035);
                if (result)
                    Console.WriteLine("添加成功");
                else
                    Console.WriteLine("添加失败");
            }
        }

        [TestMethod]
        public void Update()//插入多条数据
        {
            try
            {
                using (var db = new DbContext())
                {
                    var entity = new UserEntity
                    {
                        Id = 10034,
                        Age = 30
                    };

                    var result = db.Update(u => new { u.Age, u.ModifiedTime }, entity);

                    var result1 = db.Update("update tuser set age=@age where Id=@Id", new { Id = 10033, Age = 23 });
                    if (result)
                        Console.WriteLine("添加成功");
                    else
                        Console.WriteLine("添加失败");
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        [TestMethod]
        public void Select()
        {
            try
            {
                using (var db = new DbContext())
                {
                    var result = db.Select<UserEntity>(u => u.Valid == 1);

                    var query =
                        Query<UserEntity>.Builder(db)
                            .Select(u => new { u.Age, u.Name })
                            .Where(u => u.Age > 10 && u.Age < 30)
                            .Top(3)
                            .OrderBy(u => new { u.Age, u.Id })
                            .OrderByDesc(u => new { u.AddTime, u.Pwd });

                    var result1 = db.Select(query: query);

                    var result4 = db.Select<UserEntity>("select * from tuser where valid=@valid", new { valid = 1 });

                    var result2 = db.Count<UserEntity>(u => u.Age > 30);

                    var result3 = db.Count<UserEntity>("select count(1) from tuser where valid=@valid", new { valid = 1 });

                    //Console.WriteLine(query.Sql);

                    Console.WriteLine("查询出数据条数:");
                }
            }
            catch (Exception ex)
            {


            }

        }

        [TestMethod]
        public void Page()
        {
            try
            {
                using (var db = new DbContext())
                {
                    var result = db.Page<UserEntity>(1, 3, u => u.Valid == 1, orderExpression: u => new { u.Id });

                    Console.WriteLine("查询出数据条数:");
                }
            }
            catch (Exception ex)
            {


            }
        }

        [TestMethod]
        public void Transaction()//插入一条数据
        {
            var model = new UserEntity()
            {
                Name = "张三A",
                Pwd = "123456",
                Age = 15
            };

            using (var db = new DbContext())
            {
                var tran = db.DbTransaction;

                try
                {
                    var result = db.Insert(model, tran);
                    var result1 = db.Delete<UserEntity>(u => u.Id == 10040, tran);
                    var result2 = db.Update<UserEntity>(u => new { u.Age }, new UserEntity() { Id = 10041, Age = 50 }, tran);

                    if (result && result1 && result2)
                    { tran.Commit(); }
                    else
                    {
                        tran.Rollback();
                    }
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
    }
}
